package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TMedicalLisInfo;
import com.ruoyi.system.domain.TMedicalOutpatientdoctorResult;
import com.ruoyi.system.domain.dto.OutInfoIdsDto;
import com.ruoyi.system.domain.dto.lisinfoDto;
import com.ruoyi.system.service.ITMedicalLisInfoService;
import com.ruoyi.system.service.ITMedicalOutpatientdoctorResultService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TMedicalOutpatientdoctorReceiverecord;
import com.ruoyi.system.service.ITMedicalOutpatientdoctorReceiverecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 接诊记录Controller
 *
 * @author ruoyi
 * @date 2021-09-27
 */
@RestController
@RequestMapping("/system/receiverecord")
public class TMedicalOutpatientdoctorReceiverecordController extends BaseController {
    @Autowired
    private ITMedicalOutpatientdoctorReceiverecordService tMedicalOutpatientdoctorReceiverecordService;
    @Autowired
    private ITMedicalLisInfoService itMedicalLisInfoService;
    @Autowired
    private ITMedicalOutpatientdoctorResultService itMedicalOutpatientdoctorResultService;

    /**
     * 查询接诊记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:receiverecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMedicalOutpatientdoctorReceiverecord tMedicalOutpatientdoctorReceiverecord) {
        startPage();
        List<TMedicalOutpatientdoctorReceiverecord> list = tMedicalOutpatientdoctorReceiverecordService.selectTMedicalOutpatientdoctorReceiverecordList(tMedicalOutpatientdoctorReceiverecord);
        return getDataTable(list);
    }

    /**
     * 导出接诊记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:receiverecord:export')")
    @Log(title = "接诊记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMedicalOutpatientdoctorReceiverecord tMedicalOutpatientdoctorReceiverecord) {
        List<TMedicalOutpatientdoctorReceiverecord> list = tMedicalOutpatientdoctorReceiverecordService.selectTMedicalOutpatientdoctorReceiverecordList(tMedicalOutpatientdoctorReceiverecord);
        ExcelUtil<TMedicalOutpatientdoctorReceiverecord> util = new ExcelUtil<TMedicalOutpatientdoctorReceiverecord>(TMedicalOutpatientdoctorReceiverecord.class);
        return util.exportExcel(list, "接诊记录数据");
    }

    /**
     * 获取接诊记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:receiverecord:query')")
    @GetMapping(value = "/{opdoctorreceiverecordid}")
    public AjaxResult getInfo(@PathVariable("opdoctorreceiverecordid") String opdoctorreceiverecordid) {
        return AjaxResult.success(tMedicalOutpatientdoctorReceiverecordService.selectTMedicalOutpatientdoctorReceiverecordByOpdoctorreceiverecordid(opdoctorreceiverecordid));
    }

    /**
     * 新增接诊记录
     */
    @PreAuthorize("@ss.hasPermi('system:receiverecord:add')")
    @Log(title = "接诊记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMedicalOutpatientdoctorReceiverecord tMedicalOutpatientdoctorReceiverecord) {
        tMedicalOutpatientdoctorReceiverecord.setOpdoctorreceiverecordid(SecurityUtils.getUsername() + new Date().getTime());
//        tMedicalOutpatientdoctorReceiverecord.setDoctorid(SecurityUtils.getUsername());
        tMedicalOutpatientdoctorReceiverecord.setCreateTime(new Date());
        System.out.println(tMedicalOutpatientdoctorReceiverecord);
        return toAjax(tMedicalOutpatientdoctorReceiverecordService.insertTMedicalOutpatientdoctorReceiverecord(tMedicalOutpatientdoctorReceiverecord));
    }

    /**
     * 修改接诊记录
     */
    @PreAuthorize("@ss.hasPermi('system:receiverecord:edit')")
    @Log(title = "接诊记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMedicalOutpatientdoctorReceiverecord tMedicalOutpatientdoctorReceiverecord) {
        return toAjax(tMedicalOutpatientdoctorReceiverecordService.updateTMedicalOutpatientdoctorReceiverecord(tMedicalOutpatientdoctorReceiverecord));
    }

    /**
     * 检查
     */
    @Log(title = "接诊记录", businessType = BusinessType.INSERT)
    @PutMapping("/jiancha")
    public AjaxResult jincha(@RequestBody lisinfoDto lisinfoDto) {
        TMedicalLisInfo tm = new TMedicalLisInfo();
        tm.setId(SecurityUtils.getUsername() + new Date().getTime());
        tm.setName(lisinfoDto.getState());
        tm.setPersonid(lisinfoDto.getHuanzhe());
        tm.setDoctorid(lisinfoDto.getZhuzhiyisheng());
        tm.setCreateTime(new Date());
        tm.setStatus(2l);
        tm.setReceiverecordid(lisinfoDto.getId());
        System.out.println(tm);
        itMedicalLisInfoService.insertTMedicalLisInfo(tm);
//        tMedicalOutpatientdoctorReceiverecordService.deleteTMedicalOutpatientdoctorReceiverecordByOpdoctorreceiverecordid(lisinfoDto.getId());
        return toAjax(1);
    }

    /**
     * 诊断
     */
    @Log(title = "接诊记录", businessType = BusinessType.INSERT)
    @PutMapping("/zhenduans")
    public AjaxResult jincha(@RequestBody TMedicalOutpatientdoctorResult tMedicalOutpatientdoctorResult) {
        TMedicalOutpatientdoctorReceiverecord tm = new TMedicalOutpatientdoctorReceiverecord();
        tm.setJiezhen(4l);
        tm.setOpdoctorreceiverecordid(tMedicalOutpatientdoctorResult.getOpdoctorreceiverecordid());

        tMedicalOutpatientdoctorReceiverecordService.updateTMedicalOutpatientdoctorReceiverecord(tm);
        tMedicalOutpatientdoctorResult.setOpdoctorresultid(SecurityUtils.getUsername() + new Date().getTime());
        tMedicalOutpatientdoctorResult.setZddate(new Date());
        itMedicalOutpatientdoctorResultService.insertTMedicalOutpatientdoctorResult(tMedicalOutpatientdoctorResult);
        System.out.println(tMedicalOutpatientdoctorResult);

        return toAjax(1);
    }

    /**
     * 修改接诊记录
     */
    @Log(title = "接诊记录", businessType = BusinessType.UPDATE)
    @PutMapping("/editzhenduan")
    public AjaxResult editzhenduan(@RequestBody OutInfoIdsDto od) {

        TMedicalOutpatientdoctorReceiverecord tMedicalOutpatientdoctorReceiverecord = new TMedicalOutpatientdoctorReceiverecord();
        String[] idss = od.getIdss();//ID
        Long status = od.getStatus();//状态
        for (String id : idss) {
            tMedicalOutpatientdoctorReceiverecord.setOpdoctorreceiverecordid(id);
            tMedicalOutpatientdoctorReceiverecord.setJiezhen(status);
            tMedicalOutpatientdoctorReceiverecordService.updateTMedicalOutpatientdoctorReceiverecord(tMedicalOutpatientdoctorReceiverecord);
        }
        return toAjax(1);
    }

    /**
     * 删除接诊记录
     */
    @PreAuthorize("@ss.hasPermi('system:receiverecord:remove')")
    @Log(title = "接诊记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{opdoctorreceiverecordids}")
    public AjaxResult remove(@PathVariable String[] opdoctorreceiverecordids) {
        return toAjax(tMedicalOutpatientdoctorReceiverecordService.deleteTMedicalOutpatientdoctorReceiverecordByOpdoctorreceiverecordids(opdoctorreceiverecordids));
    }
}
